# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=sqlite-analyzer
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.16.1
pkgrel=1
pkgdesc="An analysis program SQLite database files (mingw-w64)"
url="https://www.sqlite.org/"
arch=('any')
license=(PublicDomain)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-tcl")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-fossil")
source=("LICENSE"
        "lemon.patch")
sha256sums=('0b76663a90e034f3d7f2af5bfada4cedec5ebc275361899eccc5c18e6f01ff1f'
            '11f11863934816ced106693e3b89ae8b1469ee8bff85482d01e92c5c8b8ad767')

prepare() {
  if [ ! -f sqlite.fossil ]; then
    fossil clone https://www.sqlite.org/cgi/src sqlite.fossil
  fi
  if [ ! -f .fslckout ]; then
    fossil open sqlite.fossil
  fi
  fossil update version-${pkgver}
  fossil revert
  patch -p0 -i "${srcdir}"/lemon.patch
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  CFLAGS+=" -fexceptions -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_DISABLE_DIRSYNC=1 -DSQLITE_ENABLE_RTREE=1 -fno-strict-aliasing"
  ../configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-threadsafe \
    --enable-session \
    --enable-json1 \
    --enable-fts3 \
    --enable-fts4 \
    --enable-fts5 \
    --enable-rtree \
    --with-tcl=${MINGW_PREFIX}/lib
  make sqlite3_analyzer.exe
}

package() {
  cd ${srcdir}/build-${CARCH}
  install -Dm755 .libs/sqlite3_analyzer.exe "${pkgdir}${MINGW_PREFIX}/bin/sqlite3_analyzer.exe"
  install -Dm644 "${srcdir}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
